package com.yang.listener;

import com.yang.YangSkills;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.SmallFireball;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class YangSkillOpenListener implements Listener {
    private static YangSkills yangSkills;
    public YangSkillOpenListener(YangSkills yangSkills) {
        this.yangSkills = yangSkills;
    }
    private final Map<UUID, Long> cooldowns = new HashMap<>();

    // 检查玩家是否处于冷却中
    private boolean hasCooldown(Player player) {
        if (cooldowns.containsKey(player.getUniqueId())) {
            long cooldownTime = cooldowns.get(player.getUniqueId());
            long currentTime = System.currentTimeMillis();
            return currentTime < cooldownTime;
        }
        return false;
    }

    // 设置玩家的冷却时间
    private void setCooldown(Player player, long milliseconds) {
        long cooldownTime = System.currentTimeMillis() + milliseconds;
        cooldowns.put(player.getUniqueId(), cooldownTime);
    }
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        // 检查是否是主手（左键点击）动作
        if (event.getAction().toString().contains("LEFT")) {
            // 获取玩家
            Player player = event.getPlayer();

            // 检查冷却时间
            if (!hasCooldown(player)) {
                // 处理左键点击事件
                // 在这里添加你的自定义逻辑
                player.sendMessage("你进行了左键点击攻击动作");

                // 发射火焰弹
//                startFireballTask(player);
                // 检查冷却时间并发送提示
                if (hasCooldown(player)) {
                    player.sendMessage("冷却中，请等待片刻再试！");
                    return;
                }

                // 创建火焰弹实体
                SmallFireball fireball = player.launchProjectile(SmallFireball.class);

                // 设置火焰弹的速度和方向
                fireball.setVelocity(player.getLocation().getDirection().multiply(2));

                // 在这里添加你的其他自定义逻辑，比如设置火焰弹的伤害等
                player.sendMessage("你发射了一颗火焰弹！");

                // 设置冷却时间为3秒
                setCooldown(player, 3000);
            } else {
                // 提示玩家冷却中
                player.sendMessage("冷却中，请等待片刻再试！");
            }
        }
    }

    // 处理菜单点击事件
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        if (event.getInventory().getTitle().equals("学习技能")) {
            event.setCancelled(true); // 阻止玩家移动物品
            Player player = (Player) event.getWhoClicked();
            ItemStack clickedItem = event.getCurrentItem();
            if (clickedItem != null) {
                if (clickedItem.getType() == Material.BOOK) {
                    ItemMeta itemMeta = clickedItem.getItemMeta();
                    List<String> lore = itemMeta.getLore();
                    lore.forEach(line ->{
                        player.getPlayer().sendMessage(line);
                    });
                    player.getPlayer().sendMessage(lore.get(0));
                    player.sendMessage("你点击了菜单中的钻石剑按钮！");
                    // 在这里执行与按钮相关的操作
                }
            }
        }
    }
}
